/* ring_buffer.h: Simple ring buffer API */

#ifndef __RING_BUFFER_SERIAL_H__
#define __RING_BUFFER_SERIAL_H__

#include <stdint.h>
#include "errno.h"
#include "util.h"

#ifdef __cplusplus
extern "C" {
#endif

#define SIZE32_OF(x) (sizeof((x))/sizeof(u32_t))

/**
 * @brief A structure to represent a ring buffer
 */
struct ring_buf {
	u32_t head;	 /**< Index in buf for the head element */
	u32_t tail;	 /**< Index in buf for the tail element */
	u32_t dropped_put_count; /**< Running tally of the number of failed
				     * put attempts
				     */
	u32_t size;   /**< Size of buf in 32-bit chunks */
	uint8_t *buf;	 /**< Memory region for stored entries */
	u32_t mask;   /**< Modulo mask if size is a power of 2 */
};

/**
 * @defgroup ring_buffer_apis Ring Buffer APIs
 * @ingroup kernel_apis
 * @{
 */

/**
 * @brief Statically define and initialize a standard ring buffer.
 *
 * This macro establishes a ring buffer of an arbitrary size. A standard
 * ring buffer uses modulo arithmetic operations to maintain itself.
 *
 * The ring buffer can be accessed outside the module where it is defined
 * using:
 *
 * @code extern struct ring_buf <name>; @endcode
 *
 * @param name Name of the ring buffer.
 * @param size32 Size of ring buffer (in 32-bit words).
 */
#define SYS_RING_BUF_DECLARE_SIZE(name, size32) \
	static u32_t _ring_buffer_data_##name[size32]; \
	struct ring_buf name = { \
		.size = size32, \
		.buf = _ring_buffer_data_##name \
	};

/**
 * @brief Initialize a ring buffer.
 *
 * This routine initializes a ring buffer, prior to its first use. It is only
 * used for ring buffers not defined using SYS_RING_BUF_DECLARE_POW2 or
 * SYS_RING_BUF_DECLARE_SIZE.
 *
 * Setting @a size to a power of 2 establishes a high performance ring buffer
 * that doesn't require the use of modulo arithmetic operations to maintain
 * itself.
 *
 * @param buf Address of ring buffer.
 * @param size Ring buffer size (in 32-bit words).
 * @param data Ring buffer data area (typically u32_t data[size]).
 */
static inline void sys_ring_buf_init(struct ring_buf *buf, u32_t size,
				     u8_t *data)
{
	buf->head = 0;
	buf->tail = 0;
	buf->dropped_put_count = 0;
	buf->size = size;
	buf->buf = data;
	if (is_power_of_two(size)) {
		buf->mask = size - 1;
	} else {
		buf->mask = 0;
	}
}

//static inline void sys_ring_buf_clear_empty(struct ring_buf *buf)
//{
//	buf->head = 0;
//	buf->tail = 0;
//	buf->dropped_put_count = 0;  
//}

/**
 * @brief Determine if a ring buffer is empty.
 *
 * @param buf Address of ring buffer.
 *
 * @return 1 if the ring buffer is empty, or 0 if not.
 */
static inline int sys_ring_buf_is_empty(struct ring_buf *buf)
{
	return (buf->head == buf->tail);
}

/**
 * @brief Determine free space in a ring buffer.
 *
 * @param buf Address of ring buffer.
 *
 * @return Ring buffer free space (in 32-bit words).
 */
static inline int sys_ring_buf_space_get(struct ring_buf *buf)
{
	if (sys_ring_buf_is_empty(buf)) {
		return buf->size - 1;
	}

	if (buf->tail < buf->head) {
		return buf->head - buf->tail - 1;
	}

	/* buf->tail > buf->head */
	return (buf->size - buf->tail) + buf->head - 1;
}

/**
 * @brief Determine used space in a ring buffer.
 *
 * @param buf Address of ring buffer.
 *
 * @return Ring buffer used space (in 32-bit words).
 */
static inline int sys_ring_buf_used_space_get(struct ring_buf *buf)
{
	if (sys_ring_buf_is_empty(buf)) {
		return 0;
	}

	if (buf->tail < buf->head) {
		return buf->size - (buf->head - buf->tail);
	}

	/* buf->tail > buf->head */
	return buf->tail - buf->head;
}

int sys_ring_buf_put(struct ring_buf *buf, u8_t *data, u32_t size);

int sys_ring_buf_get(struct ring_buf *buf, u8_t *data, u32_t size);

int sys_ring_buf_peek(struct ring_buf *buf, u8_t *data, u32_t size);

void sys_ring_buf_clear_empty(struct ring_buf *buf, u32_t size);

/**
 * @}
 */

#ifdef __cplusplus
}
#endif

#endif /* __RING_BUFFER_H__ */
